package com.mes.web.controller.biz;

import com.aspose.cells.PdfSaveOptions;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.*;
import com.mes.biz.domain.vo.*;
import com.mes.biz.service.*;
import com.mes.common.annotation.Log;
import com.mes.common.core.controller.BaseController;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.domain.R;
import com.mes.common.core.domain.model.LoginUser;
import com.mes.common.core.page.PageDomain;
import com.mes.common.enums.BusinessType;
import com.mes.common.exception.BizException;
import com.mes.common.utils.DateUtils;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import com.mes.common.utils.apose.LicenseUtil;
import com.mes.common.utils.poi.ExcelUtil;
import com.mes.framework.cache.DictUtils;
import com.mes.framework.web.service.TokenService;
import com.mes.system.domain.SysTenant;
import com.mes.system.service.SysTenantService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * 销售单Controller
 * @author hwg
 * @email hwg
 * @date 2023-05-15
 */
@RestController
@RequestMapping("/biz/bizOrder")
public class BizOrderController extends BaseController {

    @Autowired
    private BizOrderService bizOrderService;
    @Autowired
    private BizOrderAttachService bizOrderAttachService;
    @Autowired
    private BizOrderProductService bizOrderProductService;
    @Autowired
    private BizProductService bizProductService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private BizCustomerService bizCustomerService;
    @Autowired
    private BizAfterSaleOrderAttachService bizAfterSaleOrderAttachService;
    @Autowired
    private BizOrderMaterialService bizOrderMaterialService;

    @Autowired
    private BizOrderAccessoryService bizOrderAccessoryService;
    @Autowired
    private BizOrderInstallerService bizOrderInstallerService;
    @Autowired
    private BizCustomerPayableDetailService bizCustomerPayableDetailService;
    @Autowired
    private BizOrderProcessRecordService bizOrderProcessRecordService;
    @Autowired
    private BizConfigService bizConfigService;
    @Resource
    private SysTenantService sysTenantService;
    /**
     * 查询销售单列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrder:list')")
    @GetMapping("/list")
    public R<PageInfo> list(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        PageInfo<BizOrder> page = bizOrderService.findPage(bizOrder);
        for (BizOrder order : page.getList()) {
            order.setAuditStatus(order.getOrderStatus());
//            List<BizOrderInstaller> installerList = bizOrderInstallerService.getInstallNameByOrderId(order.getId());
//            order.setBizOrderInstallers(installerList);
//            if (StringUtils.isNotEmpty(order.getAddress())){
//                boolean b = bizOrderService.checkAddressUnique(order);
//                order.setAddressRepetition(b);
//            }else {
//                order.setAddressRepetition(false);
//            }
        }
        return R.data(page);
    }

    /**
     * 获取销售单详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrder:query')")
    @GetMapping(value = "/{id}")
    public R<BizOrder> detail(@PathVariable("id") String id) {
        BizOrder bizOrder = bizOrderService.get(id);
        bizOrder.setAuditStatus(bizOrder.getOrderStatus());
        List<BizOrderAttach> bizOrderAttachList = bizOrderAttachService.getAttachByOrderIdWithType(bizOrder.getId(),1);
        List<String> attachs = bizOrderAttachList.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
        bizOrder.setAttachList(attachs);

        List<BizOrderAttach> installAttachList = bizOrderAttachService.getAttachByOrderIdWithType(bizOrder.getId(),4);
        List<String> installAttachs = installAttachList.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
        bizOrder.setInstallerAttachList(installAttachs);

        List<BizOrderProduct> bizOrderProducts = bizOrderProductService.getBizOrderProductByOrderId(bizOrder.getId());
        for (BizOrderProduct bizOrderProduct : bizOrderProducts) {
            BizProduct bizProduct = bizProductService.get(bizOrderProduct.getProductId());
            bizOrderProduct.setBizProduct(bizProduct);
        }
        bizOrder.setProductList(bizOrderProducts);
        List<BizOrderMaterial> bizOrderMaterials = bizOrderMaterialService.getBizOrderMaterialByOrderId(bizOrder.getId());
        bizOrder.setMaterialList(bizOrderMaterials);
        return R.data(bizOrder);
    }

    /**
     * 新增销售单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrder:add')")
    @Log(title = "销售单", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated BizOrder bizOrder) {
        return R.data(bizOrderService.createOrder(bizOrder));
    }

    /**
     * 修改销售单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrder:edit')")
    @Log(title = "销售单", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated BizOrder bizOrder) {
        return R.data(bizOrderService.updateOrder(bizOrder));
    }


    @PreAuthorize("@ss.hasPermi('biz:bizOrder:edit')")
    @Log(title = "销售单", businessType = BusinessType.UPDATE)
    @PostMapping("/updateOrderHead")
    public R updateOrder(@RequestBody @Validated BizOrder bizOrder) {
        return R.data(bizOrderService.updateOrderHead(bizOrder));
    }
    /**
     * 销售单补单
     */
    @Log(title = "销售单补单", businessType = BusinessType.INSERT)
    @PostMapping("/spplement")
    public R spplement(@RequestBody @Validated BizOrder bizOrder) {
        return R.data(bizOrderService.spplementOrder(bizOrder));
    }

    /**
     * 删除销售单
     */
    @PreAuthorize("@ss.hasPermi('biz:bizOrder:remove')")
    @Log(title = "销售单", businessType = BusinessType.DELETE)
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("/{id}")
    public R remove(@PathVariable String id) {
        BizOrder bizOrder = bizOrderService.get(id);
        if (!"1".equals(bizOrder.getOrderStatus())){
            return R.fail("待审核的订单才能删除");
        }
//        boolean b = bizCustomerPayableDetailService.checkAccountCodeByOrderNumber(bizOrder.getOrderNumber());
//        if (b){
//            return R.fail("已生成对账单，不能删除");
//        }
        String orderId = bizOrder.getId();
        boolean res = bizOrderService.remove(bizOrder);
        if(res) {
            List<BizOrderAttach> bizOrderAttaches = bizOrderAttachService.getBizOrderAttachByOrderId(orderId);
            if (bizOrderAttaches != null && bizOrderAttaches.size()>0){
                String[] ids = bizOrderAttaches.stream().map(BizOrderAttach::getId).toArray(String[]::new);
                if (ids.length > 0){
                    bizOrderAttachService.deleteBizOrderAttachByIds(ids, BaseEntity.DEL_FLAG_DELETE);
                }
            }

        }
        return R.status(true);
    }

    /**
     * 根据字典类型查询字典数据信息等其他自定义信息
     */
    @GetMapping(value = "/getInitData/{dictTypes}")
    public R getInitData(@PathVariable String dictTypes) {
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.putAll(DictUtils.getMultiDictList(dictTypes));
        return R.data(dataMap);
    }

    @PreAuthorize("@ss.hasPermi('biz:bizOrder:endOrder')")
    @Log(title = "销售单", businessType = BusinessType.UPDATE)
    @GetMapping(value = "/endOrder/{id}")
    public R endOrder(@PathVariable String id) {
        return R.data(bizOrderService.endOrder(id));
    }


    /**
     * 审核
     */
    @GetMapping(value = "/audit/{id}")
    public R auditOrder(@PathVariable String id) {
        return R.data(bizOrderService.auditOrder(id));
    }

    /**
     * 领料\生产完成
     */
    @GetMapping(value = "/change/{id}")
    public R materialOrder(@PathVariable String id) {
        return R.data(bizOrderService.changeOrder(id));
    }

    /**
     * 下达生产
     */
    @PostMapping(value = "/process")
    public R processOrder(@RequestBody BizOrder bizOrder) {
        return R.data(bizOrderService.processOrder(bizOrder));
    }

    /**
     * 安装
     */
    @PostMapping(value = "/install")
    public R installOrder(@RequestBody BizOrder bizOrder) {
        return R.data(bizOrderService.installOrder(bizOrder));
    }

    /**
     * 水槽已到
     */
    @PostMapping(value = "/sink")
    public R sinkArrivalOrder(@RequestBody BizOrder bizOrder) {
        return R.data(bizOrderService.sinkArrivalOrder(bizOrder));
    }

    /**
     * 外部小程序-订单进度
     * @param bizOrder
     * @param request
     * @param response
     * @return
     */

    @GetMapping("/appletList")
    public R<PageInfo> appletList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(null != loginUser&& BaseEntity.CUSTOMER_USER.equals(loginUser.getUser().getUserType())){
            BizCustomer bizCustomer = bizCustomerService.getCustomerByUserId(loginUser.getUserId());
            bizOrder.setCustomerId(bizCustomer.getId());
        }
        PageInfo<BizOrder> page = bizOrderService.findPage(bizOrder);
        for (int i = 0; i < page.getList().size(); i++) {
            page.getList().get(i).setAuditStatus(page.getList().get(i).getOrderStatus());
//            List<BizOrderAttach> bizOrderAttachList = bizOrderAttachService.getBizOrderAttachByOrderId(page.getList().get(i).getId());
//            List<String> attachs = bizOrderAttachList.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
//            page.getList().get(i).setAttachList(attachs);
            List<BizOrderProduct> bizOrderProducts = bizOrderProductService.getBizOrderProductByOrderId(page.getList().get(i).getId());
            for (int j = 0; j < bizOrderProducts.size(); j++) {
                BizProduct bizProduct = bizProductService.get(bizOrderProducts.get(j).getProductId());
                bizOrderProducts.get(j).setProductCode(bizProduct.getProductCode());
                bizOrderProducts.get(j).setModel(bizProduct.getModel());
            }
            page.getList().get(i).setProductList(bizOrderProducts);

            List<BizOrderAttach> bizOrderAttachList = bizOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getId(),1);
            List<String> attachs = bizOrderAttachList.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
            bizOrder.setAttachList(attachs);
        }
        return R.data(page);
    }

    /**
     * 内部小程序-工序交接
     * @param bizOrder
     * @param request
     * @param response
     * @return
     */

    @GetMapping("/appletProcessList")
    public R<PageInfo> appletProcessList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        LoginUser loginUser = tokenService.getLoginUser(request);
        PageInfo<BizOrder> page = bizOrderService.appletProcessList(bizOrder,loginUser.getUserId());
        for (int i = 0; i < page.getList().size(); i++) {
            page.getList().get(i).setAuditStatus(page.getList().get(i).getOrderStatus());
            List<BizOrderAttach> bizOrderAttachList = bizOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getId(),1);
            List<String> attachs = bizOrderAttachList.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
            page.getList().get(i).setAttachList(attachs);
//            List<BizOrderProduct> bizOrderProducts = bizOrderProductService.getBizOrderProductByOrderId(page.getList().get(i).getId());
//            for (int j = 0; j < bizOrderProducts.size(); j++) {
//                BizProduct bizProduct = bizProductService.get(bizOrderProducts.get(j).getProductId());
//                bizOrderProducts.get(j).setProductCode(bizProduct.getProductCode());
//                bizOrderProducts.get(j).setModel(bizProduct.getModel());
//            }
//            page.getList().get(i).setProductList(bizOrderProducts);
        }
        return R.data(page);
    }

    /**
     * 外部小程序-售后帮助
     * @param bizOrder
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/AftermarketAppletList")
    public R<PageInfo> AftermarketAppletList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(null != loginUser&& BaseEntity.CUSTOMER_USER.equals(loginUser.getUser().getUserType())){
            BizCustomer bizCustomer = bizCustomerService.getCustomerByUserId(loginUser.getUserId());
            bizOrder.setCustomerId(bizCustomer.getId());
        }
        PageInfo<BizOrder> page = bizOrderService.AftermarketAppletList(bizOrder);
        for (int i = 0; i < page.getList().size(); i++) {
            page.getList().get(i).setAuditStatus(page.getList().get(i).getOrderStatus());
            if (StringUtils.isNotEmpty(page.getList().get(i).getAfterSaleId())){
                List<BizAfterSaleOrderAttach> customerAttachList = bizAfterSaleOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getAfterSaleId(),"1");
                List<String> customerAttachs = customerAttachList.stream().map(BizAfterSaleOrderAttach::getAttachPath).collect(Collectors.toList());
                page.getList().get(i).setCustomerAttachList(customerAttachs);

                List<BizAfterSaleOrderAttach> afterAttachList = bizAfterSaleOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getAfterSaleId(),"2");
                List<String> afterAttachs = afterAttachList.stream().map(BizAfterSaleOrderAttach::getAttachPath).collect(Collectors.toList());
                page.getList().get(i).setAfterAttachList(afterAttachs);
            }
        }
        return R.data(page);
    }

    /**
    * @Author: wy
    * @Description: 小程序-生产领料
    * @DateTime: 2023-06-25 19:54
    * @Params:
    * @Return
    */
    @GetMapping("/pickingList")
    public R<PageInfo> pickingList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        PageInfo<BizOrder> page = bizOrderService.pickingList(bizOrder);
        for (int i = 0; i < page.getList().size(); i++) {
            page.getList().get(i).setAuditStatus(page.getList().get(i).getOrderStatus());
            List<BizOrderAttach> bizOrderAttachList = bizOrderAttachService.getAttachByOrderIdWithType(page.getList().get(i).getId(),1);
            List<String> attachs = bizOrderAttachList.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
            page.getList().get(i).setAttachList(attachs);
        }
        return R.data(page);
    }

//    /**
//    * @Author: wy
//    * @Description: 设置配送或安装人员
//    * @DateTime: 2023-07-01 17:37
//    * @Params:
//    * @Return
//    */
//    @PostMapping("/staffAllocation")
//    public R staffAllocation(@RequestBody OrderInstall orderInstall) {
//        return R.data(bizOrderService.staffAllocation(orderInstall));
//    }

    /**
     *
     * @param bizOrderAccessory
     * @return
     */
    @PostMapping("/addAccessory")
    public R addAccessory(@RequestBody BizOrderAccessory bizOrderAccessory) {
        bizOrderAccessory.setTenantId(SecurityUtils.getTenantId());
        return R.status(bizOrderAccessoryService.save(bizOrderAccessory));
    }

    /**
    * @Author: wy
    * @Description: 车辆派遣/安装
    * @DateTime: 2023-07-02 15:21
    * @Params:
    * @Return
    */
    @GetMapping("/vehicleDispatchList")
    public R<PageInfo> vehicleDispatchList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        LoginUser loginUser = tokenService.getLoginUser(request);
        PageInfo<BizOrder> page = bizOrderService.vehicleDispatchList(bizOrder,loginUser.getUserId());
        for (BizOrder order : page.getList()) {
            List<BizOrderAttach> bizOrderAttaches = bizOrderAttachService.getAttachByOrderIdWithType(order.getId(), 4);
            List<String> customerAttachs = bizOrderAttaches.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
            order.setInstallerAttachList(customerAttachs);

            List<BizOrderAttach> bizOrderAttachList = bizOrderAttachService.getAttachByOrderIdWithType(order.getId(),1);
            List<String> attachs = bizOrderAttachList.stream().map(BizOrderAttach::getAttachPath).collect(Collectors.toList());
            order.setAttachList(attachs);

        }
        return R.data(page);
    }

    /**
    * @Author: wy
    * @Description: 修改成品仓库
    * @DateTime: 2023-07-02 20:13
    * @Params:
    * @Return
    */
    @PostMapping(value = "/saveWarehouse")
    public R saveWarehouse(@RequestBody BizOrder bizOrder) {
        return R.data(bizOrderService.saveWarehouse(bizOrder));
    }

    @PostMapping(value = "/shipments")
    public R shipments(@RequestBody BizOrder bizOrder) {
        return R.data(bizOrderService.shipments(bizOrder));
    }

    /**
     * 查询销售单明细列表
     */
    @GetMapping("/listOrderStat")
    public R<PageInfo> listOrderStat(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        PageInfo<OrderVo> page = bizOrderService.listOrderStatPage(bizOrder);
        return R.data(page);
    }

    /**
     * 打印销售明细单
     */
    @GetMapping("/printOrderStat")
    public void export(BizOrder bizOrder, HttpServletResponse response) {
        List<OrderVo> list = bizOrderService.listOrderStat(bizOrder);
        ExcelUtil<OrderVo> util = new ExcelUtil<>(OrderVo.class);
        util.printExcel(response,list, "销售明细表");
    }

    @PostMapping(value = "/returnProcess")
    public R returnProcessOrder(@RequestBody BizOrder bizOrder) {
        return R.data(bizOrderService.returnProcessOrder(bizOrder));
    }

    /**
     * 订单完成
     * @param bizOrder
     * @return
     */
    @PostMapping(value = "/complete")
    public R complete(@RequestBody BizOrder bizOrder) {
        return R.data(bizOrderService.complete(bizOrder));
    }

    /**
     * 销售订单追踪列表
     * @param bizOrder
     * @param request
     * @param response
     * @return
     */
    @GetMapping("/orderTrackingList")
    public R<PageInfo> orderTrackingList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        PageInfo<OrderTrackingVo> page = bizOrderService.orderTrackingList(bizOrder);
        for (OrderTrackingVo order : page.getList()) {
            List<OrderTrackingRecord> orderTrackingRecordList = bizOrderProcessRecordService.findCompletedByOrderProductId(order.getOrderProductId(),null);
            order.setOrderTrackingRecordList(orderTrackingRecordList);
        }
        return R.data(page);
    }

    @Log(title = "生产计件")
    @GetMapping("/pieceProductionList")
    public R<PageInfo> pieceProductionList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        bizOrder.setPage(new PageDomain(request, response));
        PageInfo<PieceProductionVo> page = bizOrderService.pieceProductionList(bizOrder);
        for (PieceProductionVo order : page.getList()) {
            List<PieceProductionRecordVo> orderTrackingRecordList = bizOrderProcessRecordService.findPieceProductionDetailList(order.getOrderProductId(),bizOrder.getCreator());
            order.setPieceProductionRecordVos(orderTrackingRecordList);
        }
        return R.data(page);
    }

    @Log(title = "生产计件")
    @GetMapping("/pieceProductionExportList")
    public List<PieceProductionVo> pieceProductionExportList(BizOrder bizOrder, HttpServletRequest request, HttpServletResponse response) {
        List<PieceProductionVo> list = bizOrderService.printPieceProductionList(bizOrder);
        for (PieceProductionVo order : list) {
            List<PieceProductionRecordVo> orderTrackingRecordList = bizOrderProcessRecordService.findPieceProductionDetailList(order.getOrderProductId(),bizOrder.getCreator());
            order.setPieceProductionRecordVos(orderTrackingRecordList);
        }
        return list;
    }

    @Log(title = "生产计件打印", businessType = BusinessType.EXPORT)
    @GetMapping("printPieceProductionList")
    public void export(HttpServletResponse response, BizOrder bizOrder) throws Exception {
        List<PieceProductionVo> list = bizOrderService.printPieceProductionList(bizOrder);
        for (PieceProductionVo order : list) {
            List<PieceProductionRecordVo> orderTrackingRecordList = bizOrderProcessRecordService.findPieceProductionDetailList(order.getOrderProductId(),bizOrder.getCreator());
            order.setPieceProductionRecordVos(orderTrackingRecordList);
        }
        ExcelUtil<PieceProductionVo> util = new ExcelUtil<>(PieceProductionVo.class);
        util.printEasyExcel(response,list, "销售明细表", PieceProductionVo.class);
    }

    @Log(title = "生产计件打印", businessType = BusinessType.EXPORT)
    @GetMapping("checkAddressUnique")
    public boolean checkAddressUnique(BizOrder bizOrder) throws Exception {
        return bizOrderService.checkAddressUnique(bizOrder);
    }


    @Log(title = "订购单打印", businessType = BusinessType.EXPORT)
    @GetMapping("orderPrint")
    public void orderPrint(HttpServletResponse response, BizOrder bizOrder) throws Exception {
        //创建HSSFWorkbook对象(excel的文档对象)
        LicenseUtil.GetLicense();
        HSSFWorkbook wb = new HSSFWorkbook();
        OutputStream out=null;
        try {
            if (StringUtils.isEmpty(bizOrder.getId())){
                throw new BizException("请选择订购单");
            }
            BizOrder order = bizOrderService.findById(bizOrder);
            //创建sheet对象（excel的表单）
            HSSFSheet sheet = wb.createSheet("订购单");
            //设置列宽
            sheet.setColumnWidth(0, 5000);
            sheet.setColumnWidth(1, 6000);
            sheet.setColumnWidth(2, 5000);
            sheet.setColumnWidth(3, 4000);
            sheet.setColumnWidth(4, 5000);
            sheet.setColumnWidth(5, 4000);
            sheet.setColumnWidth(6, 4000);
            sheet.setColumnWidth(7, 4000);
            sheet.setColumnWidth(8, 4000);
            sheet.setColumnWidth(9, 6000);
            sheet.setColumnWidth(10, 4000);

            //标题字体
            HSSFFont titleFont = wb.createFont();
            titleFont.setFontName("宋体");
            titleFont.setFontHeightInPoints((short) 14);// 字体大小
            //标题样式
            HSSFCellStyle titleStyle = wb.createCellStyle();
            titleStyle.setFont(titleFont);
            titleStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            titleStyle.setBorderTop(BorderStyle.THIN); // 上边框
            titleStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            titleStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            titleStyle.setBorderRight(BorderStyle.THIN); // 右边框
            titleStyle.setLocked(true);

            //标题标注字体
            HSSFFont titleFont2 = wb.createFont();
            titleFont2.setFontName("宋体");
            titleFont2.setFontHeightInPoints((short) 12);// 字体大小
            //标题标注样式
            HSSFCellStyle titleStyle2 = wb.createCellStyle();
            titleStyle2.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            titleStyle2.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            titleStyle2.setBorderTop(BorderStyle.THIN); // 上边框
            titleStyle2.setBorderBottom(BorderStyle.THIN); // 下边框
            titleStyle2.setBorderLeft(BorderStyle.THIN); // 左边框
            titleStyle2.setBorderRight(BorderStyle.THIN); // 右边框
            titleStyle2.setFont(titleFont2);

            //表头字体
            HSSFFont headerFont = wb.createFont();
            headerFont.setFontName("宋体");
            headerFont.setFontHeightInPoints((short) 12);// 字体大小
            //表头样式
            HSSFCellStyle headerStyle = wb.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            headerStyle.setBorderTop(BorderStyle.THIN); // 上边框
            headerStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            headerStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            headerStyle.setBorderRight(BorderStyle.THIN); // 右边框
            headerStyle.setFont(headerFont);

            //数据字体
            HSSFFont font = wb.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 11);// 字体大小
            //数据样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setWrapText(true);// 自动换行
            style.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            style.setBorderTop(BorderStyle.THIN); // 上边框
            style.setBorderBottom(BorderStyle.THIN); // 下边框
            style.setBorderLeft(BorderStyle.THIN); // 左边框
            style.setBorderRight(BorderStyle.THIN); // 右边框
            style.setFont(font);

            //数据字体
            HSSFFont font2 = wb.createFont();
            font2.setFontName("宋体");
            font2.setFontHeightInPoints((short) 14);// 字体大小
            //数据样式
            HSSFCellStyle style2 = wb.createCellStyle();
            style2.setWrapText(true);// 自动换行
            style2.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            style2.setFont(font);

            //表头
            String[] columnNames = {"序号","区域","型号", "颜色","规格", "数量", "单位", "单价", "金额", "备注"};
            String fileName = DateUtils.dateTime()+"订购单.xlsx";
            BizConfig configKey = bizConfigService.getByConfigKey(BaseEntity.SALES_REMARK);
            if (configKey == null){
                throw new Exception("未添加订购单备注，请联系管理员");
            }
            String remark =configKey.getRemark();
            int startRow = 0;
            if (order != null) {
                //获取账套名称
                SysTenant sysTenant = sysTenantService.get(getLoginUser().getUser().getTenantId());
                String tenantName =  sysTenant.getTenantName();

                //合并单元格
                CellRangeAddress cellRangeAddress = new CellRangeAddress(startRow, startRow, 0, 9);
                sheet.addMergedRegion(cellRangeAddress);

                //创建第一行
                HSSFRow row1 = sheet.createRow(startRow);
                //设置行高
                row1.setHeight((short) 800);
                //创建第一列
                HSSFCell cell = row1.createCell(0);
                cell.setCellValue(tenantName+"-订购单");
                cell.setCellStyle(titleStyle);
                row1.setRowStyle(titleStyle);

                //创建第二行
                //客户及订单编号
                HSSFRow row2 = sheet.createRow(startRow + 1);
                row2.setHeight((short) 600);
                HSSFCell cell20 = row2.createCell(0);
                cell20.setCellValue("订单编号");
                cell20.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses1 = new CellRangeAddress(startRow + 1, startRow + 1, 1, 2);
                sheet.addMergedRegion(cellAddresses1);
                HSSFCell cell21 = row2.createCell(1);
                cell21.setCellValue(order.getOrderNumber());
                cell21.setCellStyle(titleStyle2);
                HSSFCell cell8 = row2.createCell(2);
                cell8.setCellStyle(style);
                HSSFCell cell12 = row2.createCell(3);
                cell12.setCellStyle(style);
                cell12.setCellValue("客户名称");
                //合并单元格
                CellRangeAddress cellAddresses2 = new CellRangeAddress(startRow + 1, startRow + 1, 4, 6);
                sheet.addMergedRegion(cellAddresses2);
                HSSFCell cell10 = row2.createCell(4);
                cell10.setCellValue(order.getCustomerName());
                cell10.setCellStyle(style);
                HSSFCell cell11 = row2.createCell(5);
                cell11.setCellStyle(style);
                HSSFCell cell14 = row2.createCell(6);
                cell14.setCellStyle(style);
                HSSFCell cell27 = row2.createCell(7);
                cell27.setCellValue("送货地址");
                cell27.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses3 = new CellRangeAddress(startRow + 1, startRow + 1, 8, 9);
                sheet.addMergedRegion(cellAddresses3);
                HSSFCell cell28 = row2.createCell(8);
                cell28.setCellStyle(titleStyle2);
                cell28.setCellValue(order.getAddress());
                HSSFCell cell19 = row2.createCell(9);
                cell19.setCellStyle(titleStyle2);
//                HSSFCell cell22 = row2.createCell(10);
//                cell22.setCellStyle(titleStyle2);

                //创建第三行
                HSSFRow row3 = sheet.createRow(startRow + 2);
                row3.setHeight((short) 600);
                HSSFCell cell23 = row3.createCell(0);
                cell23.setCellValue("业主电话");
                cell23.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses4 = new CellRangeAddress(startRow + 2, startRow + 2, 1, 2);
                sheet.addMergedRegion(cellAddresses4);
                HSSFCell cell24 = row3.createCell(1);
                cell24.setCellValue(order.getPhone());
                cell24.setCellStyle(titleStyle2);
                HSSFCell cell25 = row3.createCell(2);
                cell25.setCellStyle(style);
                HSSFCell cell26 = row3.createCell(3);
                cell26.setCellStyle(style);
                cell26.setCellValue("安装时间");
                //合并单元格
                CellRangeAddress cellAddresses5 = new CellRangeAddress(startRow + 2, startRow + 2, 4, 6);
                sheet.addMergedRegion(cellAddresses5);
                HSSFCell cell29 = row3.createCell(4);
                cell29.setCellValue(order.getAddress());
                cell29.setCellStyle(style);
                HSSFCell cell30 = row3.createCell(5);
                cell30.setCellStyle(style);
                HSSFCell cell31 = row3.createCell(6);
                cell31.setCellStyle(style);
                HSSFCell cell32 = row3.createCell(7);
                cell32.setCellValue("业务员");
                cell32.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses6 = new CellRangeAddress(startRow + 2, startRow + 2, 8, 9);
                sheet.addMergedRegion(cellAddresses6);
                HSSFCell cell33 = row3.createCell(8);
                cell33.setCellStyle(titleStyle2);
                cell33.setCellValue(order.getCustomernanName());
                HSSFCell cell34 = row3.createCell(9);
                cell34.setCellStyle(titleStyle2);
//                HSSFCell cell35 = row3.createCell(10);
//                cell35.setCellStyle(titleStyle2);

                //创建第四行
                HSSFRow row4 = sheet.createRow(startRow + 3);
                row4.setHeight((short) 600);
                HSSFCell cell18 = row4.createCell(0);
                cell18.setCellValue("配送模式");
                cell18.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses7 = new CellRangeAddress(startRow + 3, startRow + 3, 1, 2);
                sheet.addMergedRegion(cellAddresses7);
                HSSFCell cell36 = row4.createCell(1);
                cell36.setCellValue(order.getDeliveryType().equals("1") ? "客户自提" :"工厂配送");
                cell36.setCellStyle(titleStyle2);
                HSSFCell cell37 = row4.createCell(2);
                cell37.setCellStyle(style);
                HSSFCell cell38 = row4.createCell(3);
                cell38.setCellStyle(style);
                cell38.setCellValue("安装模式");
                //合并单元格
                CellRangeAddress cellAddresses8 = new CellRangeAddress(startRow + 3, startRow + 3, 4, 6);
                sheet.addMergedRegion(cellAddresses8);
                HSSFCell cell39 = row4.createCell(4);
                cell39.setCellValue(order.getInstallType().equals("1") ? "无安装" :"包安装");
                cell39.setCellStyle(style);
                HSSFCell cell40 = row4.createCell(5);
                cell40.setCellStyle(style);
                HSSFCell cell41 = row4.createCell(6);
                cell41.setCellStyle(style);
                HSSFCell cell42 = row4.createCell(7);
//                cell42.setCellValue("安装模式");
                cell42.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses9 = new CellRangeAddress(startRow + 3, startRow + 3, 7, 9);
                sheet.addMergedRegion(cellAddresses9);
                HSSFCell cell43 = row4.createCell(8);
                cell43.setCellStyle(titleStyle2);
//                cell43.setCellValue(order.getInstallType().equals("1") ? "无安装" :"包安装");
                HSSFCell cell44 = row4.createCell(9);
                cell44.setCellStyle(titleStyle2);
//                HSSFCell cell45 = row4.createCell(10);
//                cell45.setCellStyle(titleStyle2);


//                //创建第五行
//                HSSFRow row5 = sheet.createRow(startRow + 4);
//                row5.setHeight((short) 600);
//                HSSFCell cell46 = row5.createCell(0);
//                cell46.setCellValue("开单时间");
//                cell46.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses10 = new CellRangeAddress(startRow + 4, startRow + 4, 1, 2);
//                sheet.addMergedRegion(cellAddresses10);
//                HSSFCell cell47 = row5.createCell(1);
//                cell47.setCellValue(DateUtils.dateTime(order.getReceiveDate()));
//                cell47.setCellStyle(titleStyle2);
//                HSSFCell cell48 = row5.createCell(2);
//                cell48.setCellStyle(style);
//                HSSFCell cell49 = row5.createCell(3);
//                cell49.setCellStyle(style);
//                cell49.setCellValue("配送模式");
//                //合并单元格
//                CellRangeAddress cellAddresses11 = new CellRangeAddress(startRow + 4, startRow + 4, 4, 6);
//                sheet.addMergedRegion(cellAddresses11);
//                HSSFCell cell51 = row5.createCell(4);
//                cell51.setCellValue(order.getDeliveryType().equals("1") ? "客户自提" :"工厂配送");
//                cell51.setCellStyle(style);
//                HSSFCell cell52 = row5.createCell(5);
//                cell52.setCellStyle(style);
//                HSSFCell cell53 = row5.createCell(6);
//                cell53.setCellStyle(style);
//                HSSFCell cell54 = row5.createCell(7);
//                cell54.setCellValue("安装模式");
//                cell54.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses12 = new CellRangeAddress(startRow + 4, startRow + 4, 8, 10);
//                sheet.addMergedRegion(cellAddresses12);
//                HSSFCell cell55 = row5.createCell(8);
//                cell55.setCellStyle(titleStyle2);
//                cell55.setCellValue(order.getInstallType().equals("1") ? "无安装" :"包安装");
//                HSSFCell cell56 = row5.createCell(9);
//                cell56.setCellStyle(titleStyle2);
//                HSSFCell cell57 = row5.createCell(10);
//                cell57.setCellStyle(titleStyle2);
//
//
//                //创建第六行
//                HSSFRow row6 = sheet.createRow(startRow + 5);
//                row6.setHeight((short) 600);
//                HSSFCell cell58 = row6.createCell(0);
//                cell58.setCellValue("备注1");
//                cell58.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses13 = new CellRangeAddress(startRow + 5, startRow + 5, 1, 2);
//                sheet.addMergedRegion(cellAddresses13);
//                HSSFCell cell59 = row6.createCell(1);
//                cell59.setCellValue(order.getRemark());
//                cell59.setCellStyle(titleStyle2);
//                HSSFCell cell60 = row6.createCell(2);
//                cell60.setCellStyle(style);
//                HSSFCell cell61 = row6.createCell(3);
//                cell61.setCellStyle(style);
//                cell61.setCellValue("备注2");
//                //合并单元格
//                CellRangeAddress cellAddresses14 = new CellRangeAddress(startRow + 5, startRow + 5, 4, 6);
//                sheet.addMergedRegion(cellAddresses14);
//                HSSFCell cell62 = row6.createCell(4);
//                cell62.setCellValue(order.getRemarkA());
//                cell62.setCellStyle(style);
//                HSSFCell cell63 = row6.createCell(5);
//                cell63.setCellStyle(style);
//                HSSFCell cell64 = row6.createCell(6);
//                cell64.setCellStyle(style);
//                HSSFCell cell65 = row6.createCell(7);
//                cell65.setCellValue("备注3");
//                cell65.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses15 = new CellRangeAddress(startRow + 5, startRow + 5, 8, 10);
//                sheet.addMergedRegion(cellAddresses15);
//                HSSFCell cell66 = row6.createCell(8);
//                cell66.setCellStyle(titleStyle2);
//                cell66.setCellValue(order.getRemarkB());
//                HSSFCell cell67 = row6.createCell(9);
//                cell67.setCellStyle(titleStyle2);
//                HSSFCell cell68 = row6.createCell(10);
//                cell68.setCellStyle(titleStyle2);

                //表头
                HSSFRow row7 = sheet.createRow(startRow + 4);
                row7.setHeight((short) 600);
                for (int k = 0; k < columnNames.length; k++) {
                    HSSFCell cell3 = row7.createCell(k);
                    cell3.setCellValue(columnNames[k]);
                    cell3.setCellStyle(headerStyle);// 自动换行
                }

                int size = 0;
                //内容
                List<BizOrderProduct> detailList = bizOrderProductService.getProductByOrderId(order.getId());
                BigDecimal countAmount = BigDecimal.ZERO;
                if (!CollectionUtils.isEmpty(detailList)) {
                    size = detailList.size();
                    for (int j = 0; j < detailList.size(); j++) {
                        BizOrderProduct bizOrderProduct = detailList.get(j);
                        HSSFRow row = sheet.createRow(startRow + 4 + j + 1);
                        row.setHeight((short) 600);
                        HSSFCell cell13 = row.createCell(0);
                        cell13.setCellValue(bizOrderProduct.getSort()+1);
                        cell13.setCellStyle(style);// 自动换行

                        HSSFCell cell1 = row.createCell(1);
                        cell1.setCellValue(bizOrderProduct.getArea());
                        cell1.setCellStyle(style);// 自动换行

                        HSSFCell cell9 = row.createCell(2);
                        cell9.setCellValue(bizOrderProduct.getModel());
                        cell9.setCellStyle(style);// 自动换行

                        HSSFCell cell2 = row.createCell(3);
                        cell2.setCellValue(bizOrderProduct.getColor());
                        cell2.setCellStyle(style);// 自动换行

                        HSSFCell cell3 = row.createCell(4);
                        cell3.setCellValue(bizOrderProduct.getSpec());
                        cell3.setCellStyle(style);

                        HSSFCell cell4 = row.createCell(5);
                        cell4.setCellValue(bizOrderProduct.getNumber().toString());
                        cell4.setCellStyle(style);

//                        HSSFCell cell15 = row.createCell(6);
//                        cell15.setCellValue(bizOrderProduct.getCoefficient().toString());
//                        cell15.setCellStyle(style);

                        HSSFCell cell16 = row.createCell(6);
                        cell16.setCellValue(bizOrderProduct.getUnitName());
                        cell16.setCellStyle(style);

                        HSSFCell cell5 = row.createCell(7);
                        cell5.setCellValue(bizOrderProduct.getPrice() != null ? bizOrderProduct.getPrice().toString(): "0");
                        cell5.setCellStyle(style);

                        HSSFCell cell6 = row.createCell(8);
                        cell6.setCellValue(bizOrderProduct.getAmount() !=null ? bizOrderProduct.getAmount().toString() :"0");
                        cell6.setCellStyle(style);

                        HSSFCell cell7 = row.createCell(9);
                        cell7.setCellValue(bizOrderProduct.getRemark());
                        cell7.setCellStyle(style);
                        countAmount = countAmount.add(bizOrderProduct.getAmount());
                    }

                }
                HSSFRow rowk2 = sheet.createRow(startRow + 4 + size + 1);
                rowk2.setHeight((short) 600);
                HSSFCell cell1 = rowk2.createCell(0);
                cell1.setCellValue("合计金额");
                cell1.setCellStyle(style);
                HSSFCell cell4 = rowk2.createCell(1);
                cell4.setCellStyle(style);
                HSSFCell cell5 = rowk2.createCell(2);
                cell5.setCellStyle(style);
                HSSFCell cell6 = rowk2.createCell(3);
                cell6.setCellStyle(style);
                HSSFCell cell7 = rowk2.createCell(4);
                cell7.setCellStyle(style);
                HSSFCell cell9 = rowk2.createCell(5);
                cell9.setCellStyle(style);
                HSSFCell cell15 = rowk2.createCell(6);
                cell15.setCellStyle(style);
                HSSFCell cell2 = rowk2.createCell(7);
                cell2.setCellStyle(style);
                HSSFCell cell13 = rowk2.createCell(8);
                cell13.setCellStyle(style);
                cell13.setCellValue(countAmount.toString());
                HSSFCell cell16 = rowk2.createCell(9);
                cell16.setCellStyle(style);
//                HSSFCell cell17 = rowk2.createCell(10);
//                cell17.setCellStyle(style);
                //合并单元格
                CellRangeAddress cellAddresses = new CellRangeAddress(startRow + 4 + size + 2, startRow + 4 + size + 2, 0, 9);
                sheet.addMergedRegion(cellAddresses);

                //创建最后一行
                HSSFRow row = sheet.createRow(startRow + 4 + size + 2);

                //设置行高
                row.setHeight((short) 2000);
                //创建第一列
                HSSFCell cell3 = row.createCell(0);
                cell3.setCellValue(remark);
                cell3.setCellStyle(style2);
            }


            //输出Excel文件
            out = response.getOutputStream();
            response.reset();
//            //设置响应头
//            response.setContentType("application/octet-stream");
//            fileName = new String(fileName.getBytes("gbk"),"ISO8859_1");
//            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\"");
//            wb.write(output);
//            output.close();
            try {
                out = new FileOutputStream(ExcelUtil.getAbsoluteFile(fileName));
            } catch (Exception e) {
                e.printStackTrace();
            }
            wb.write(out);

            com.aspose.cells.Workbook wb2 = new com.aspose.cells.Workbook(ExcelUtil.getAbsoluteFile(fileName));
            PdfSaveOptions opts = new PdfSaveOptions();
            opts.setOnePagePerSheet(true);
            response.setContentType("application/pdf");
            //   response.setHeader("Content-Disposition", "attachment; filename=“.pdf");
            wb2.save(response.getOutputStream(),opts);

            IOUtils.closeQuietly(wb);
            IOUtils.closeQuietly(out);
//            return R.success(fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
//       return null;
    }

    @Log(title = "订购单打印", businessType = BusinessType.EXPORT)
    @GetMapping("orderPrintExcel")
    public R orderPrintExcel(HttpServletResponse response, BizOrder bizOrder) throws Exception {
        //创建HSSFWorkbook对象(excel的文档对象)
        LicenseUtil.GetLicense();
        HSSFWorkbook wb = new HSSFWorkbook();
        OutputStream out=null;
        try {
            if (StringUtils.isEmpty(bizOrder.getId())){
                throw new BizException("请选择订购单");
            }
            BizOrder order = bizOrderService.findById(bizOrder);
            //创建sheet对象（excel的表单）
            HSSFSheet sheet = wb.createSheet("订购单");
            //设置列宽
            sheet.setColumnWidth(0, 5000);
            sheet.setColumnWidth(1, 6000);
            sheet.setColumnWidth(2, 5000);
            sheet.setColumnWidth(3, 4000);
            sheet.setColumnWidth(4, 5000);
            sheet.setColumnWidth(5, 4000);
            sheet.setColumnWidth(6, 4000);
            sheet.setColumnWidth(7, 4000);
            sheet.setColumnWidth(8, 4000);
            sheet.setColumnWidth(9, 6000);
            sheet.setColumnWidth(10, 4000);

            //标题字体
            HSSFFont titleFont = wb.createFont();
            titleFont.setFontName("宋体");
            titleFont.setFontHeightInPoints((short) 14);// 字体大小
            //标题样式
            HSSFCellStyle titleStyle = wb.createCellStyle();
            titleStyle.setFont(titleFont);
            titleStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            titleStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            titleStyle.setBorderTop(BorderStyle.THIN); // 上边框
            titleStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            titleStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            titleStyle.setBorderRight(BorderStyle.THIN); // 右边框
            titleStyle.setLocked(true);

            //标题标注字体
            HSSFFont titleFont2 = wb.createFont();
            titleFont2.setFontName("宋体");
            titleFont2.setFontHeightInPoints((short) 12);// 字体大小
            //标题标注样式
            HSSFCellStyle titleStyle2 = wb.createCellStyle();
            titleStyle2.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            titleStyle2.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            titleStyle2.setBorderTop(BorderStyle.THIN); // 上边框
            titleStyle2.setBorderBottom(BorderStyle.THIN); // 下边框
            titleStyle2.setBorderLeft(BorderStyle.THIN); // 左边框
            titleStyle2.setBorderRight(BorderStyle.THIN); // 右边框
            titleStyle2.setFont(titleFont2);

            //表头字体
            HSSFFont headerFont = wb.createFont();
            headerFont.setFontName("宋体");
            headerFont.setFontHeightInPoints((short) 12);// 字体大小
            //表头样式
            HSSFCellStyle headerStyle = wb.createCellStyle();
            headerStyle.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            headerStyle.setBorderTop(BorderStyle.THIN); // 上边框
            headerStyle.setBorderBottom(BorderStyle.THIN); // 下边框
            headerStyle.setBorderLeft(BorderStyle.THIN); // 左边框
            headerStyle.setBorderRight(BorderStyle.THIN); // 右边框
            headerStyle.setFont(headerFont);

            //数据字体
            HSSFFont font = wb.createFont();
            font.setFontName("宋体");
            font.setFontHeightInPoints((short) 11);// 字体大小
            //数据样式
            HSSFCellStyle style = wb.createCellStyle();
            style.setWrapText(true);// 自动换行
            style.setAlignment(HorizontalAlignment.CENTER);// 左右居中
            style.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            style.setBorderTop(BorderStyle.THIN); // 上边框
            style.setBorderBottom(BorderStyle.THIN); // 下边框
            style.setBorderLeft(BorderStyle.THIN); // 左边框
            style.setBorderRight(BorderStyle.THIN); // 右边框
            style.setFont(font);

            //数据字体
            HSSFFont font2 = wb.createFont();
            font2.setFontName("宋体");
            font2.setFontHeightInPoints((short) 14);// 字体大小
            //数据样式
            HSSFCellStyle style2 = wb.createCellStyle();
            style2.setWrapText(true);// 自动换行
            style2.setVerticalAlignment(VerticalAlignment.CENTER);// 上下居中
            style2.setFont(font);

            //表头
            String[] columnNames = {"序号","区域","型号", "颜色","规格", "数量", "单位", "单价", "金额", "备注"};
            String fileName = DateUtils.dateTime()+"订购单.xlsx";
            BizConfig configKey = bizConfigService.getByConfigKey(BaseEntity.SALES_REMARK);
            if (configKey == null){
                throw new Exception("未添加订购单备注，请联系管理员");
            }
            String remark =configKey.getRemark();
            int startRow = 0;
            if (order != null) {
                //获取账套名称
                SysTenant sysTenant = sysTenantService.get(getLoginUser().getUser().getTenantId());
                String tenantName =  sysTenant.getTenantName();

                //合并单元格
                CellRangeAddress cellRangeAddress = new CellRangeAddress(startRow, startRow, 0, 9);
                sheet.addMergedRegion(cellRangeAddress);

                //创建第一行
                HSSFRow row1 = sheet.createRow(startRow);
                //设置行高
                row1.setHeight((short) 800);
                //创建第一列
                HSSFCell cell = row1.createCell(0);
                cell.setCellValue(tenantName+"-订购单");
                cell.setCellStyle(titleStyle);
                row1.setRowStyle(titleStyle);

                //创建第二行
                //客户及订单编号
                HSSFRow row2 = sheet.createRow(startRow + 1);
                row2.setHeight((short) 600);
                HSSFCell cell20 = row2.createCell(0);
                cell20.setCellValue("订单编号");
                cell20.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses1 = new CellRangeAddress(startRow + 1, startRow + 1, 1, 2);
                sheet.addMergedRegion(cellAddresses1);
                HSSFCell cell21 = row2.createCell(1);
                cell21.setCellValue(order.getOrderNumber());
                cell21.setCellStyle(titleStyle2);
                HSSFCell cell8 = row2.createCell(2);
                cell8.setCellStyle(style);
                HSSFCell cell12 = row2.createCell(3);
                cell12.setCellStyle(style);
                cell12.setCellValue("客户名称");
                //合并单元格
                CellRangeAddress cellAddresses2 = new CellRangeAddress(startRow + 1, startRow + 1, 4, 6);
                sheet.addMergedRegion(cellAddresses2);
                HSSFCell cell10 = row2.createCell(4);
                cell10.setCellValue(order.getCustomerName());
                cell10.setCellStyle(style);
                HSSFCell cell11 = row2.createCell(5);
                cell11.setCellStyle(style);
                HSSFCell cell14 = row2.createCell(6);
                cell14.setCellStyle(style);
                HSSFCell cell27 = row2.createCell(7);
                cell27.setCellValue("送货地址");
                cell27.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses3 = new CellRangeAddress(startRow + 1, startRow + 1, 8, 9);
                sheet.addMergedRegion(cellAddresses3);
                HSSFCell cell28 = row2.createCell(8);
                cell28.setCellStyle(titleStyle2);
                cell28.setCellValue(order.getAddress());
                HSSFCell cell19 = row2.createCell(9);
                cell19.setCellStyle(titleStyle2);
//                HSSFCell cell22 = row2.createCell(10);
//                cell22.setCellStyle(titleStyle2);

                //创建第三行
                HSSFRow row3 = sheet.createRow(startRow + 2);
                row3.setHeight((short) 600);
                HSSFCell cell23 = row3.createCell(0);
                cell23.setCellValue("业主电话");
                cell23.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses4 = new CellRangeAddress(startRow + 2, startRow + 2, 1, 2);
                sheet.addMergedRegion(cellAddresses4);
                HSSFCell cell24 = row3.createCell(1);
                cell24.setCellValue(order.getPhone());
                cell24.setCellStyle(titleStyle2);
                HSSFCell cell25 = row3.createCell(2);
                cell25.setCellStyle(style);
                HSSFCell cell26 = row3.createCell(3);
                cell26.setCellStyle(style);
                cell26.setCellValue("安装时间");
                //合并单元格
                CellRangeAddress cellAddresses5 = new CellRangeAddress(startRow + 2, startRow + 2, 4, 6);
                sheet.addMergedRegion(cellAddresses5);
                HSSFCell cell29 = row3.createCell(4);
                cell29.setCellValue(order.getAddress());
                cell29.setCellStyle(style);
                HSSFCell cell30 = row3.createCell(5);
                cell30.setCellStyle(style);
                HSSFCell cell31 = row3.createCell(6);
                cell31.setCellStyle(style);
                HSSFCell cell32 = row3.createCell(7);
                cell32.setCellValue("业务员");
                cell32.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses6 = new CellRangeAddress(startRow + 2, startRow + 2, 8, 9);
                sheet.addMergedRegion(cellAddresses6);
                HSSFCell cell33 = row3.createCell(8);
                cell33.setCellStyle(titleStyle2);
                cell33.setCellValue(order.getCustomernanName());
                HSSFCell cell34 = row3.createCell(9);
                cell34.setCellStyle(titleStyle2);
//                HSSFCell cell35 = row3.createCell(10);
//                cell35.setCellStyle(titleStyle2);

                //创建第四行
                HSSFRow row4 = sheet.createRow(startRow + 3);
                row4.setHeight((short) 600);
                HSSFCell cell18 = row4.createCell(0);
                cell18.setCellValue("配送模式");
                cell18.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses7 = new CellRangeAddress(startRow + 3, startRow + 3, 1, 2);
                sheet.addMergedRegion(cellAddresses7);
                HSSFCell cell36 = row4.createCell(1);
                cell36.setCellValue(order.getDeliveryType().equals("1") ? "客户自提" :"工厂配送");
                cell36.setCellStyle(titleStyle2);
                HSSFCell cell37 = row4.createCell(2);
                cell37.setCellStyle(style);
                HSSFCell cell38 = row4.createCell(3);
                cell38.setCellStyle(style);
                cell38.setCellValue("安装模式");
                //合并单元格
                CellRangeAddress cellAddresses8 = new CellRangeAddress(startRow + 3, startRow + 3, 4, 6);
                sheet.addMergedRegion(cellAddresses8);
                HSSFCell cell39 = row4.createCell(4);
                cell39.setCellValue(order.getInstallType().equals("1") ? "无安装" :"包安装");
                cell39.setCellStyle(style);
                HSSFCell cell40 = row4.createCell(5);
                cell40.setCellStyle(style);
                HSSFCell cell41 = row4.createCell(6);
                cell41.setCellStyle(style);
                HSSFCell cell42 = row4.createCell(7);
//                cell42.setCellValue("安装模式");
                cell42.setCellStyle(titleStyle2);
                //合并单元格
                CellRangeAddress cellAddresses9 = new CellRangeAddress(startRow + 3, startRow + 3, 7, 9);
                sheet.addMergedRegion(cellAddresses9);
                HSSFCell cell43 = row4.createCell(8);
                cell43.setCellStyle(titleStyle2);
//                cell43.setCellValue(order.getInstallType().equals("1") ? "无安装" :"包安装");
                HSSFCell cell44 = row4.createCell(9);
                cell44.setCellStyle(titleStyle2);
//                HSSFCell cell45 = row4.createCell(10);
//                cell45.setCellStyle(titleStyle2);


//                //创建第五行
//                HSSFRow row5 = sheet.createRow(startRow + 4);
//                row5.setHeight((short) 600);
//                HSSFCell cell46 = row5.createCell(0);
//                cell46.setCellValue("开单时间");
//                cell46.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses10 = new CellRangeAddress(startRow + 4, startRow + 4, 1, 2);
//                sheet.addMergedRegion(cellAddresses10);
//                HSSFCell cell47 = row5.createCell(1);
//                cell47.setCellValue(DateUtils.dateTime(order.getReceiveDate()));
//                cell47.setCellStyle(titleStyle2);
//                HSSFCell cell48 = row5.createCell(2);
//                cell48.setCellStyle(style);
//                HSSFCell cell49 = row5.createCell(3);
//                cell49.setCellStyle(style);
//                cell49.setCellValue("配送模式");
//                //合并单元格
//                CellRangeAddress cellAddresses11 = new CellRangeAddress(startRow + 4, startRow + 4, 4, 6);
//                sheet.addMergedRegion(cellAddresses11);
//                HSSFCell cell51 = row5.createCell(4);
//                cell51.setCellValue(order.getDeliveryType().equals("1") ? "客户自提" :"工厂配送");
//                cell51.setCellStyle(style);
//                HSSFCell cell52 = row5.createCell(5);
//                cell52.setCellStyle(style);
//                HSSFCell cell53 = row5.createCell(6);
//                cell53.setCellStyle(style);
//                HSSFCell cell54 = row5.createCell(7);
//                cell54.setCellValue("安装模式");
//                cell54.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses12 = new CellRangeAddress(startRow + 4, startRow + 4, 8, 10);
//                sheet.addMergedRegion(cellAddresses12);
//                HSSFCell cell55 = row5.createCell(8);
//                cell55.setCellStyle(titleStyle2);
//                cell55.setCellValue(order.getInstallType().equals("1") ? "无安装" :"包安装");
//                HSSFCell cell56 = row5.createCell(9);
//                cell56.setCellStyle(titleStyle2);
//                HSSFCell cell57 = row5.createCell(10);
//                cell57.setCellStyle(titleStyle2);
//
//
//                //创建第六行
//                HSSFRow row6 = sheet.createRow(startRow + 5);
//                row6.setHeight((short) 600);
//                HSSFCell cell58 = row6.createCell(0);
//                cell58.setCellValue("备注1");
//                cell58.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses13 = new CellRangeAddress(startRow + 5, startRow + 5, 1, 2);
//                sheet.addMergedRegion(cellAddresses13);
//                HSSFCell cell59 = row6.createCell(1);
//                cell59.setCellValue(order.getRemark());
//                cell59.setCellStyle(titleStyle2);
//                HSSFCell cell60 = row6.createCell(2);
//                cell60.setCellStyle(style);
//                HSSFCell cell61 = row6.createCell(3);
//                cell61.setCellStyle(style);
//                cell61.setCellValue("备注2");
//                //合并单元格
//                CellRangeAddress cellAddresses14 = new CellRangeAddress(startRow + 5, startRow + 5, 4, 6);
//                sheet.addMergedRegion(cellAddresses14);
//                HSSFCell cell62 = row6.createCell(4);
//                cell62.setCellValue(order.getRemarkA());
//                cell62.setCellStyle(style);
//                HSSFCell cell63 = row6.createCell(5);
//                cell63.setCellStyle(style);
//                HSSFCell cell64 = row6.createCell(6);
//                cell64.setCellStyle(style);
//                HSSFCell cell65 = row6.createCell(7);
//                cell65.setCellValue("备注3");
//                cell65.setCellStyle(titleStyle2);
//                //合并单元格
//                CellRangeAddress cellAddresses15 = new CellRangeAddress(startRow + 5, startRow + 5, 8, 10);
//                sheet.addMergedRegion(cellAddresses15);
//                HSSFCell cell66 = row6.createCell(8);
//                cell66.setCellStyle(titleStyle2);
//                cell66.setCellValue(order.getRemarkB());
//                HSSFCell cell67 = row6.createCell(9);
//                cell67.setCellStyle(titleStyle2);
//                HSSFCell cell68 = row6.createCell(10);
//                cell68.setCellStyle(titleStyle2);

                //表头
                HSSFRow row7 = sheet.createRow(startRow + 4);
                row7.setHeight((short) 600);
                for (int k = 0; k < columnNames.length; k++) {
                    HSSFCell cell3 = row7.createCell(k);
                    cell3.setCellValue(columnNames[k]);
                    cell3.setCellStyle(headerStyle);// 自动换行
                }

                int size = 0;
                //内容
                List<BizOrderProduct> detailList = bizOrderProductService.getProductByOrderId(order.getId());
                BigDecimal countAmount = BigDecimal.ZERO;
                if (!CollectionUtils.isEmpty(detailList)) {
                    size = detailList.size();
                    for (int j = 0; j < detailList.size(); j++) {
                        BizOrderProduct bizOrderProduct = detailList.get(j);
                        HSSFRow row = sheet.createRow(startRow + 4 + j + 1);
                        row.setHeight((short) 600);
                        HSSFCell cell13 = row.createCell(0);
                        cell13.setCellValue(bizOrderProduct.getSort()+1);
                        cell13.setCellStyle(style);// 自动换行

                        HSSFCell cell1 = row.createCell(1);
                        cell1.setCellValue(bizOrderProduct.getArea());
                        cell1.setCellStyle(style);// 自动换行

                        HSSFCell cell9 = row.createCell(2);
                        cell9.setCellValue(bizOrderProduct.getModel());
                        cell9.setCellStyle(style);// 自动换行

                        HSSFCell cell2 = row.createCell(3);
                        cell2.setCellValue(bizOrderProduct.getColor());
                        cell2.setCellStyle(style);// 自动换行

                        HSSFCell cell3 = row.createCell(4);
                        cell3.setCellValue(bizOrderProduct.getSpec());
                        cell3.setCellStyle(style);

                        HSSFCell cell4 = row.createCell(5);
                        cell4.setCellValue(bizOrderProduct.getNumber().toString());
                        cell4.setCellStyle(style);

//                        HSSFCell cell15 = row.createCell(6);
//                        cell15.setCellValue(bizOrderProduct.getCoefficient().toString());
//                        cell15.setCellStyle(style);

                        HSSFCell cell16 = row.createCell(6);
                        cell16.setCellValue(bizOrderProduct.getUnitName());
                        cell16.setCellStyle(style);

                        HSSFCell cell5 = row.createCell(7);
                        cell5.setCellValue(bizOrderProduct.getPrice() != null ? bizOrderProduct.getPrice().toString(): "0");
                        cell5.setCellStyle(style);

                        HSSFCell cell6 = row.createCell(8);
                        cell6.setCellValue(bizOrderProduct.getAmount() !=null ? bizOrderProduct.getAmount().toString() :"0");
                        cell6.setCellStyle(style);

                        HSSFCell cell7 = row.createCell(9);
                        cell7.setCellValue(bizOrderProduct.getRemark());
                        cell7.setCellStyle(style);
                        countAmount = countAmount.add(bizOrderProduct.getAmount());
                    }

                }
                HSSFRow rowk2 = sheet.createRow(startRow + 4 + size + 1);
                rowk2.setHeight((short) 600);
                HSSFCell cell1 = rowk2.createCell(0);
                cell1.setCellValue("合计金额");
                cell1.setCellStyle(style);
                HSSFCell cell4 = rowk2.createCell(1);
                cell4.setCellStyle(style);
                HSSFCell cell5 = rowk2.createCell(2);
                cell5.setCellStyle(style);
                HSSFCell cell6 = rowk2.createCell(3);
                cell6.setCellStyle(style);
                HSSFCell cell7 = rowk2.createCell(4);
                cell7.setCellStyle(style);
                HSSFCell cell9 = rowk2.createCell(5);
                cell9.setCellStyle(style);
                HSSFCell cell15 = rowk2.createCell(6);
                cell15.setCellStyle(style);
                HSSFCell cell2 = rowk2.createCell(7);
                cell2.setCellStyle(style);
                HSSFCell cell13 = rowk2.createCell(8);
                cell13.setCellStyle(style);
                cell13.setCellValue(countAmount.toString());
                HSSFCell cell16 = rowk2.createCell(9);
                cell16.setCellStyle(style);
//                HSSFCell cell17 = rowk2.createCell(10);
//                cell17.setCellStyle(style);
                //合并单元格
                CellRangeAddress cellAddresses = new CellRangeAddress(startRow + 4 + size + 2, startRow + 4 + size + 2, 0, 9);
                sheet.addMergedRegion(cellAddresses);

                //创建最后一行
                HSSFRow row = sheet.createRow(startRow + 4 + size + 2);

                //设置行高
                row.setHeight((short) 2000);
                //创建第一列
                HSSFCell cell3 = row.createCell(0);
                cell3.setCellValue(remark);
                cell3.setCellStyle(style2);
            }


            //输出Excel文件
            out = response.getOutputStream();
            response.reset();
//            //设置响应头
//            response.setContentType("application/octet-stream");
//            fileName = new String(fileName.getBytes("gbk"),"ISO8859_1");
//            response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".xls" + "\"");
//            wb.write(output);
//            output.close();
            try {
                out = new FileOutputStream(ExcelUtil.getAbsoluteFile(fileName));
            } catch (Exception e) {
                e.printStackTrace();
            }
            wb.write(out);
            IOUtils.closeQuietly(wb);
            IOUtils.closeQuietly(out);
            return R.success(fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
       return null;
    }


    @PostMapping(value = "/updateStock")
    public R updateStock(@RequestBody BizOrder bizOrder) {
        BizProduct bizProduct=new BizProduct();
        bizProduct.setInventoryFlag("1");
        List<BizProduct> list = bizProductService.findList(bizProduct);
        if (list !=null && list.size()>0){
            for (BizProduct product : list) {

            }

        }

        return R.data(bizOrderService.returnProcessOrder(bizOrder));
    }
}
